import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import LoginView from '../views/LoginView.vue'
import IndexView from '../views/admin/IndexView.vue'
import { loginStatus } from '@/api/admin/user'
import store from "@/store"

Vue.use(VueRouter)


const routes = [
  {
    path: '/',
    name: 'home',
    redirect: '/index',
    component: HomeView,
    children: [
      {
        path: "/index",
        component: IndexView
      }
    ]
  },
  {
    path: "/login",
    component: LoginView
  }
]

const adminRoute = [
  {
    path: "/department",
    component: HomeView,
    redirect: "/department/index",
    children: [
      {
        path: '/department/index',
        component: () => import("../views/admin/DepartmentView.vue"),
        meta: {
          title: "系管理",
          role: 1
        }
      }
    ]
  },
  {
    path: "/major",
    component: HomeView,
    redirect: "/major/index",
    children: [
      {
        path: '/major/index',
        component: () => import("../views/admin/MajorView.vue"),
        meta: {
          title: "专业管理",
          role: 2
        }
      }
    ]
  },
  {
    path: "/class",
    component: HomeView,
    redirect: "/class/index",
    children: [
      {
        path: '/class/index',
        component: () => import("../views/admin/ClassView.vue"),
        meta: {
          title: "班级管理",
          role: 4
        }
      }
    ]
  },
  {
    path: "/student",
    component: HomeView,
    redirect: "/student/index",
    children: [
      {
        path: '/student/index',
        component: () => import("../views/admin/StudentView.vue"),
        meta: {
          title: "学生管理",
          role: 8
        }
      }
    ]
  },
  {
    path: "/teacher",
    component: HomeView,
    redirect: "/teacher/index",
    children: [
      {
        path: '/teacher/index',
        component: () => import("../views/admin/TeacherView.vue"),
        meta: {
          title: "老师管理",
          role: 16
        }
      }
    ]
  },
  {
    path: "/course",
    component: HomeView,
    redirect: "/course/index",
    children: [
      {
        path: '/course/index',
        component: () => import("../views/admin/CourseView.vue"),
        meta: {
          title: "课程管理",
          role: 32
        }
      }
    ]
  },
  {
    path: "/stucourse",
    component: HomeView,
    redirect: "/stucourse/index",
    children: [
      {
        path: '/stucourse/index',
        component: () => import("../views/admin/StuCourseView.vue"),
        meta: {
          title: "选课管理",
          role: 64
        }
      }
    ]
  },
  {
    path: "/admin",
    component: HomeView,
    redirect: "/admin/index",
    children: [
      {
        path: '/admin/index',
        component: () => import("../views/admin/AdminView.vue"),
        meta: {
          title: "管理员管理",
          role: 128
        }
      }
    ]
  }
]


const studentRoute = [
  {
    path: "/select",
    component: HomeView,
    redirect: "/select/index",
    children: [
      {
        path: '/select/index',
        component: () => import("../views/student/selectView.vue"),
        meta: {
          title: "选修课程"
        }
      }
    ]
  },
  {
    path: "/withdraw",
    component: HomeView,
    redirect: "/withdraw/index",
    children: [
      {
        path: '/withdraw/index',
        component: () => import("../views/student/withdrawView.vue"),
        meta: {
          title: "学生课程"
        }
      }
    ]
  },
  {
    path: "/schedule",
    component: HomeView,
    redirect: "/schedule/index",
    children: [
      {
        path: '/schedule/index',
        component: () => import("../views/student/scheduleView.vue"),
        meta: {
          title: "课表查询"
        }
      }
    ]
  },
  {
    path: "/score",
    component: HomeView,
    redirect: "/score/index",
    children: [
      {
        path: '/score/index',
        component: () => import("../views/student/scoreView.vue"),
        meta: {
          title: "成绩查询"
        }
      }
    ]
  },
  {
    path: "/info",
    component: HomeView,
    redirect: "/info/index",
    children: [
      {
        path: '/info/index',
        component: () => import("../views/student/infoView.vue"),
        meta: {
          title: "信息维护"
        }
      }
    ]
  }
]

const teacherRoute = [
  {
    path: "/search",
    component: HomeView,
    redirect: "/score/index",
    children: [
      {
        path: '/search/index',
        component: () => import("../views/teacher/searchView.vue"),
        meta: {
          title: "授课查询"
        }
      }
    ]
  },
  {
    path: "/list",
    component: HomeView,
    redirect: "/list/index",
    children: [
      {
        path: '/list/index',
        component: () => import("../views/teacher/listView.vue"),
        meta: {
          title: "教师课表"
        }
      }
    ]
  },
  {
    path: "/tscore",
    component: HomeView,
    redirect: "/tscore/index",
    children: [
      {
        path: '/Tscore/index',
        component: () => import("../views/teacher/scoreView.vue"),
        meta: {
          title: "成绩录入"
        }
      }
    ]
  },

]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach(async (to, from, next) => {
  // console.log(from)

  //判断登录标志是否为真
  if (store.state.user.loggedIn) {
    if (to.path == "/login") {
      next('/')
    } else {
      if (store.state.user.roles.length == 0) {
        if (store.state.user.userType == 3) {
          // store.commit("user/updateRoles", adminRoute)
          // router.addRoutes(adminRoute)
          let newRoles = await store.dispatch("user/asyncRoles", adminRoute)
          router.addRoutes(newRoles)
        } else if (store.state.user.userType == 2) {
          store.commit("user/updateRoles", teacherRoute)
          router.addRoutes(teacherRoute)
        } else if (store.state.user.userType == 1) {
          store.commit("user/updateRoles", studentRoute)
          router.addRoutes(studentRoute)
        }
        store.commit("user/updateLoggedIn", false)
        next({ ...to, replace: true })
      } else {
        next()
      }
    }
  } else {
    let ret = await loginStatus()
    let loggedIn = ret.data.loggedIn

    if (loggedIn) {
      store.commit("user/updateLoggedIn")
      store.commit("user/updateUserType", ret.data.userType)
      store.commit("user/updatePermission", ret.data.permission)
      next({ ...to, replace: true })
    } else {
      if (to.path == "/login") {
        next()
      } else {
        next("/login")
      }
    }
  }
})

export default router
